package com.come4loves.game.module.actor.helper;

import io.netty.channel.ChannelHandlerContext;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.come4loves.core.comunicate.protocol.Response;
import com.come4loves.core.utils.ObjectReference;
import com.come4loves.game.proto.ActorProto;
import com.come4loves.game.proto.ModuleName;
import com.come4loves.game.proto.UserCmd;
import com.come4loves.game.server.session.PlayerContext;

/**
 * 用户消息推送帮助类
 * @author ludd
 *
 */
@Component
public class UserPushHelper {
	@Autowired
	PlayerContext playerSession;
	
	private static ObjectReference<UserPushHelper> ref = new ObjectReference<UserPushHelper>();

	@PostConstruct
	protected void init() {
		ref.set(this);
	}
	
	/**
	 * 推送用户登陆失败
	 * @param session
	 * @param statusCode
	 */
	public static void userLoginFail(ChannelHandlerContext session,Response response, short statusCode) {
		response.setStatusCode(statusCode);
		ref.get().playerSession.push(session, response);
	}
	
	/**
	 * 推送用户登陆成功
	 * @param session
	 * @param loginResult
	 */
	public static void userLoginSucces(ChannelHandlerContext session, Response response,String uid,int serverId, long actorId, String reconnectId) {
		ActorProto.UserLoginResponse.Builder builder = ActorProto.UserLoginResponse.newBuilder();
		builder.setUid(uid);
		builder.setServerId(serverId);
		builder.setActorId(actorId);
		builder.setReconnectId(reconnectId);
		response.setValue( builder.build().toByteArray());
		ref.get().playerSession.push(session, response);
	}
	
	public static void kickoff(ChannelHandlerContext session, short statusCode) {
		Response response = Response.valueOf((byte)ModuleName.Module.USER_VALUE, (byte)UserCmd.User.PUSH_KICK_VALUE, 0, statusCode);
		ref.get().playerSession.pushAndClose(session, response);
	}
	
}
